package com.zss.rocketmq.producer.common.responseEntity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * Created by wangzhenyu on 2018/4/2.
 * 统一的返回结果
 */
@ApiModel
public class ResultVO<T> implements Serializable {

    private static final long serialVersionUID = 74704857074895138L;

    //0成功
    @ApiModelProperty(value = "结果状态码，0成功，其他code为失败")
    private int code;
    @ApiModelProperty(value = "结果描述，code为0时描述为success")
    private String msg;
    @ApiModelProperty(value = "业务数据，为空时不用解析")
    private T result;

    public ResultVO(){}

    public ResultVO(T result){
        this(ResultCodeEnum.SUCCESS,result);
    }


    public ResultVO(Integer resultCodeEnum, String msg, T result){
        this.result = result;
        this.code = resultCodeEnum;
        this.msg = msg;
    }


    public ResultVO(ResultCodeEnum resultCodeEnum, T result){
        this.result=result;
        this.code=resultCodeEnum.getCode();
        this.msg=resultCodeEnum.getMsg();
    }

    public ResultVO(ResultCodeEnum resultCodeEnum, String msg, T result){
        this.result = result;
        this.code = resultCodeEnum.getCode();
        this.msg = msg;
    }

    public ResultVO(ResultCodeEnum resultCodeEnum, String msg){
        this.code = resultCodeEnum.getCode();
        this.msg = msg;
    }
    /**
     * 构造响应信息，无data
     * @param resultCodeEnum
     * @author: hbt
     */
    public ResultVO(ResultCodeEnum resultCodeEnum){
        this.code = resultCodeEnum.getCode();
        this.msg = resultCodeEnum.getMsg();
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static<T> ResultVO<T> createSuccess(T result){
        return new ResultVO(result);
    }
    
    public static<T> ResultVO<T> createError(ResultCodeEnum resultCodeEnum, String msg){
        return new ResultVO<T>(resultCodeEnum,msg);
    }


    /**
     * 获取响应信息，根据code，无data
     * @author: hbt
     */
    public static ResultVO createResponse(Integer code, String msg) {
        return new ResultVO(code, msg, null);
    }
    /**
     * 获取响应信息，根据传递的reseponseEnum，无data
     * @author: hbt
     */
    public static ResultVO createResponse(ResultCodeEnum resultCodeEnum) {
        return new ResultVO(resultCodeEnum);
    }
    /**
     * @desc: 获取成功信息，数据为空
     * @author: hbt
     */
    public static ResultVO createSuccess() {
        return new ResultVO(ResultCodeEnum.SUCCESS);
    }

    /**
     * @desc: 获取成功信息，数据为空
     * @author: hbt
     */
    public static ResultVO createError() {
        return new ResultVO(ResultCodeEnum.ERROR);
    }
    /**
     * @desc: 获取失败信息，数据为空
     * @author: hbt
     */
    public static ResultVO createError(String error) {
        return new ResultVO(ResultCodeEnum.ERROR.getCode(), error, null);
    }
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }
}
